package com.itycu.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.*;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itycu.server.annotation.Log;
import com.itycu.server.core.controller.BaseController;
import com.itycu.server.core.domain.AjaxResult;
import com.itycu.server.enums.BusinessType;
import com.itycu.server.domain.business.PraSale;
import com.itycu.server.dto.business.PraSaleDto;
import com.itycu.server.service.business.PraSaleService;
import com.itycu.server.utils.poi.ExcelUtil;
import com.itycu.server.core.page.TableDataInfo;

/**
 * 销售订单Controller
 *
 * @author itycu
 * @date 2025-02-18
 */
@Api(value = "销售订单",tags = {"销售订单接口"})
@RestController
@RequestMapping("/business/sale")
public class PraSaleController extends BaseController {
    @Resource
    private PraSaleService praSaleService;

    /**
     * 查询销售订单列表
     */
    @ApiOperation("查询销售订单列表")
    @PreAuthorize("@ss.hasPermi('business:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(PraSale praSale) {
        startPage();
        List<PraSaleDto> list = praSaleService.list(praSale);
        return getDataTable(list);
    }

    /**
     * 销售订单保存，id有值时更新，无值时新增
     */
    @ApiOperation(value = "销售订单保存，id有值时更新，无值时新增")
    @PreAuthorize("@ss.hasPermi('business:sale:save')")
    @Log(title = "销售订单", businessType = BusinessType.SAVE)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody PraSale praSale) {
        return toAjax(praSaleService.save(praSale), praSale);
    }

    /**
     * 删除销售订单
     */
    @ApiOperation("根据ID删除销售订单")
    @PreAuthorize("@ss.hasPermi('business:sale:remove')")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(praSaleService.deleteById(id));
    }

    /**
     * 批量删除销售订单
     */
    @ApiOperation("根据ID数组批量删除销售订单")
    @PreAuthorize("@ss.hasPermi('business:sale:remove')")
    @Log(title = "销售订单", businessType = BusinessType.BATCHDEL)
    @DeleteMapping("/batchDel")
    public AjaxResult batchDel(@RequestBody @ApiParam(value = "id数组", required = true) List<Long> ids) {
        int count = 0;
        if (!CollectionUtils.isEmpty(ids)) {
            count = praSaleService.deleteByIds(ids);
        }
        return toAjax(count);
    }

    /**
     * 根据id获取销售订单详细信息
     */
    @ApiOperation("根据id获取销售订单详细信息")
    @PreAuthorize("@ss.hasPermi('business:sale:query')")
    @GetMapping("/findById/{id}")
    public AjaxResult findById(@PathVariable("id") Long id) {
        return AjaxResult.success(praSaleService.findById(id));
    }

    /**
     * 导出销售订单列表
     */
    @ApiOperation("导出销售订单列表")
    @PreAuthorize("@ss.hasPermi('business:sale:export')")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PraSale praSale) {
        List<PraSaleDto> list = praSaleService.list(praSale);
        ExcelUtil<PraSaleDto> util = new ExcelUtil<PraSaleDto>(PraSaleDto.class);
        util.exportExcel(response, list, "销售订单数据");
    }
}
